package cn.dglydrpy.study.j2ee.servlet.day08;

import java.io.IOException;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * 读取 绑定到上下文的数据 和 绑定到session上的数据
 */
public class SomeServlet3 extends HttpServlet {
	private static final long serialVersionUID = 1L;

	
	protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		/*
		 * 通过继承自GenericServlet提供的方法来获取上下文
		 * 
		 */
		//得到上下文绑定的数据(关闭浏览器后再重新获取可以得到值)
		ServletContext context = getServletContext();
		System.out.println(context);
		String food = (String) context.getAttribute("food");
		
		//得到session绑定的数据(关闭浏览器后再重新获取不能得到值)
		HttpSession session = request.getSession();
		String user = (String) session.getAttribute("user");
		System.out.println(user+"爱吃"+food);
		
		//读取全局的初始化参数
		System.out.println("全局初始化参数： "+context.getInitParameter("全局初始化参数"));
	}

}
